﻿var path = this.path;
define(['dojo/_base/declare', 'geomap/plugins/layers/CantonStatisticsLayer'], function (
    declare,
    BaseCantonStatisticsLayer
) {
    var CantonStatisticsLayer = declare([BaseCantonStatisticsLayer], {
        setData: function (mapView, layer) {
            var options = layer.layer || {};
            var data = $.extend(true, [], options.data);
            this.mapView = mapView;
            var mapUtils = mapView.mapUtils;
            this.mapUtils = mapUtils;
            this.fieldArray = options.fieldArray || [
                {
                    key: 'name',
                    style: 'font-weight: bold;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;',
                },
                { key: 'count', style: '' },
            ];
            this.cantonLevelDictionary = options.cantonLevelDictionary || {
                1: { min: 0, max: 4617150 }, //国
                2: { min: 4617150, max: 2308575 }, //省
                3: { min: 2308575, max: 577144 }, //市
                4: { min: 577144, max: 144286 }, //区县
                5: { min: 144286, max: 72143 }, //乡镇
                6: { min: 72143, max: 0 }, //村
            };
            var symbol = options.symbol || {};

            this.style = mapUtils.$.extend(
                {
                    width: '60px',
                    height: '60px',
                    color: '#fff',
                    display: 'flex',
                    'background-color': '#2d8cf0',
                    'border-radius': '50%',
                    'flex-direction': 'column',
                    'justify-content': 'center',
                    'text-align': 'center',
                },
                symbol.style
            );
            this.style = mapUtils.objectToString(this.style);

            this.class = symbol.class;

            this.loaded = true;

            this.lngFieldName = options.lngFieldName;
            this.latFieldName = options.latFieldName;
            this.cantonCodeFieldName = options.cantonCodeFieldName;
            this.parentCantonCodeFieldName = options.parentCantonCodeFieldName;
            this.cantonLevelFieldName = options.cantonLevelFieldName;
            this.clickCallback = options.clickCallback;
            this.imageUrl = path + './../../one-map/1.0.0/src/mode/arcgis/css/images/square.svg';
            data.map(function (item) {
                if (item.symbol && item.symbol.style) {
                    item.symbol.style = mapUtils.$.extend(
                        {
                            width: '60px',
                            height: '60px',
                            color: '#fff',
                            display: 'flex',
                            'background-color': '#2d8cf0',
                            'border-radius': '50%',
                            'flex-direction': 'column',
                            'justify-content': 'center',
                            'text-align': 'center',
                        },
                        item.symbol.style
                    );
                    item.symbol.style = mapUtils.objectToString(item.symbol.style);
                }
            });
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            this.lngFieldName = 'x';
            this.latFieldName = 'y';
            this.data = lngLatArray;
            this.convertSr(mapView, lngLatArray, () => {
                this.__refresh();
                if (typeof options.callback == 'function') {
                    options.callback(layer);
                }
            });
        },

        getGraphicLocation: function (mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            if (!mapView) {
                return;
            }

            var geometry = esriEvent.graphic.geometry || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
        convertSr: function (mapView, lngLatArray, callback) {
            var me = this;
            if (!mapView) {
                return;
            }
            var mapUtils = mapView.mapUtils;
            var mapSr = mapView.getSpatialReference();
            me.mapSr = mapSr;
            var layerSr = me.spatialReference;
            if (mapSr == layerSr) {
                callback();
                return;
            }
            /*坐标转换，扩展的图层不像svg可以自动转换坐标，如果点位的坐标与底图的坐标不一致需要先手工转换坐标*/
            if (mapSr.wkid == '102100') {
                if (layerSr.wkid == '4326' || layerSr.wkid == '4490') {
                    me.lngLatToXY(mapView, lngLatArray, function (lngLatArray) {
                        if ($.isFunction(callback)) {
                            callback(lngLatArray, mapSr);
                        }
                    });
                    return;
                }
            }
            if (mapSr.wkid == '4326' || mapSr.wkid == '4490') {
                if (layerSr.wkid == '102100') {
                    me.xyToLngLat(mapView, lngLatArray, function (lngLatArray) {
                        if ($.isFunction(callback)) {
                            callback(lngLatArray, mapSr);
                        }
                    });
                    return;
                }
            }
            if ($.isFunction(callback)) {
                callback(lngLatArray, me.spatialReference);
            }
        },
        lngLatToXY: function (mapView, lngLatArray, callback) {
            mapView.mapUtils.webMercatorUtils(mapView, lngLatArray, {
                isLngLat: true,
                callback: callback,
            });
        },
        xyToLngLat: function (mapView, data, callback) {
            mapView.mapUtils.webMercatorUtils(mapView, lngLatArray, {
                isLngLat: false,
                callback: callback,
            });
        },
    });
    return CantonStatisticsLayer;
});
